package com.example.ota_ble.permiss


import android.Manifest
import android.app.Activity
import android.content.pm.PackageManager
import android.os.Build
import android.util.Log
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
/**
 *      申请权限，对外接口  check_need_premissionList，结果需要在调用地方实现回调函数onRequestPermissionsResult处理
 *
 *
 * */




object permissionRsq {

    var TAG = "rocjan.permissionRsq"

    private var wait_rsq_premission_list = ArrayList<String>()

    val REQUEST_CODE:Int = 1000


    private var need_premission_ble:Array<String> = arrayOf(
        Manifest.permission.BLUETOOTH,
        Manifest.permission.BLUETOOTH_ADMIN,
        Manifest.permission.BLUETOOTH_SCAN,
        Manifest.permission.BLUETOOTH_ADVERTISE,
        Manifest.permission.BLUETOOTH_CONNECT,
        Manifest.permission.ACCESS_FINE_LOCATION
    )
    private var need_premission_sys:Array<String> = arrayOf(
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
    )



    private fun request_without_permission(activity: Activity)
    {
        ActivityCompat.requestPermissions(activity,wait_rsq_premission_list.toArray(arrayOf<String>()),REQUEST_CODE)
    }

    fun check_need_premissionList(activity: Activity)
    {
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M) {
            for(idx in this.need_premission_ble.indices)
            {
                if(PackageManager.PERMISSION_GRANTED!=ContextCompat.checkSelfPermission(activity,need_premission_ble[idx]))
                {
                    wait_rsq_premission_list.add(need_premission_ble[idx])
                }
            }
            for(idx in this.need_premission_sys.indices)
            {
                if(PackageManager.PERMISSION_GRANTED!=ContextCompat.checkSelfPermission(activity,need_premission_sys[idx]))
                {
                    wait_rsq_premission_list.add(need_premission_sys[idx])
                }
            }

            if(wait_rsq_premission_list.size>0)
            {
                request_without_permission(activity)
            }

        }

    }






}